<template>
  <view>
    <!-- 商品列表 -->
    <view v-for="(item,index) in goodsList" :key="index" @click="goToDetail(item)">
      <my-goods :item="item"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          query: '',
          cid: '',
          pagesize: 10,
          pagenum: 1,
        },
        // 商品列表数据存储
        total: 0,
        goodsList: [],
        isLoad: false
      };
    },
    methods: {
      async getGoodsList(stop) {
        this.isLoad = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        if (res.meta.status !== 200) {
          return uni.showMsg('获取商品列表数据失败！')
        } else {
          this.goodsList = [...this.goodsList, ...res.message.goods]
          this.total = res.message.total
          this.isLoad = false
          stop && stop()
        }
      },
      // 跳转至商品详情页
      goToDetail(item){
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 下拉触底加载更多
      onReachBottom() {
        if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
          return uni.showMsg('数据已加载完毕！')
        } else {
          if (!this.isLoad) {
            this.queryObj.pagenum++
            this.getGoodsList()
          }
        }
      },
      // 上拉刷新
      onPullDownRefresh() {
        // 重置所有数据
        this.queryObj.pagenum = 1
        this.total = 0
        this.isLoad = false
        this.goodsList = []
        // 重新请求数据
        this.getGoodsList(()=> uni.stopPullDownRefresh())
      },
    },
    onLoad(options) {
      // console.log('options', options)
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // 获取商品列表数据
      this.getGoodsList()
    },
  }
</script>

<style lang="scss">

</style>
